﻿using System;
using System.Web.UI;
using SiteUnity.Layers.Application.BaseClasses;
using SiteUnity.Frame.Common.UITools;
using SiteUnity.Layers.Application;

namespace SiteUnity.Frame.Modules
{
    [ToolboxData("<{0}:TwitterStatus runat=server/>")]
    public class TwitterStatus : BaseWebControl
    {
        public bool HideTitle { get; set; }
        public string Username { get; set; }
        public ContainerType HtmlTag { get; set; }
        public int Count { get; set; }
          
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (string.IsNullOrEmpty(Username))
                Username = SiteManager.FindWebsiteSettingByKey("Twitter-Username").Value;
            if (HtmlTag == default(ContainerType))
                HtmlTag = ContainerType.h2;
            if (Count == default(int))
                Count = 10;
        }

        protected override void OnPreRender(EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), this.ClientID, "var divTwitterStatus='" + Username + "TwitterStatus" + "';", true);
            Page.ClientScript.RegisterClientScriptInclude(typeof(Page), this.ClientID + "2", "http://twitter.com/statuses/user_timeline/" + Username + ".json?callback=twitterCallback&count=" + Count);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!HideTitle)
            {
                Container header = new Container();
                header.Tag = HtmlTag;
                header.InnerText = "Twitter";
                this.Controls.Add(header);
            }
            Container list = new Container();
            list.ID = Username + "TwitterStatus";
            list.Tag = ContainerType.div;
            this.Controls.Add(list);

            foreach (Control control in this.Controls)
                control.RenderControl(writer);
        }
    }
}
